
/*
This is for cash slate

*/


var browser=navigator.appName;
if (browser=='Microsoft Internet Explorer'){var IE = true}
else var IE = false

var businesses_info = {} //business name:list of branches
  
$(document).ready(function()
{

    var get_businesses_info = function(){
        doajax('get_businesses_info',{},function(data){
                businesses_info = data.businesses_info;
                //confirm($.toJSON(businesses_info))
            })
    }


       
    $('#station_open').click(function(){
        station_open();
    });
    
    $('#loginout').click(function(){
        rs = db.execute('select * from branch_info');
        var d = fetchall(rs);
        confirm($.toJSON(d))
    });

    $('#clear_db').click(function(){
        clear_db();
    });

//LOAD CONFIG FILE   
    $('#load_data').click(function(){
        var data = $('#config_data').val();
        config_load(data);
    });
    
    var config_load = function(rdata){
        var data = parse_bulk(rdata);
        var bsname = data['business'];
        var brname = data['name'];
        // action,ndata string,list of data names
        doajax('add_business',{'bsname':bsname},function(){  
            doajax('add_branch',{'bsname':bsname,'brname':brname},function(){  
                for(var i=0;i<data['managers'].length;i++){ 
                    var d = data['managers'][i].split('|')
                    var td = {'ename':d[0],'brname':brname,'bsname':bsname,'idcode':d[1],'pwcode':d[2],
                            'permissions':['view','create','edit'],'emptype':'manager'}
                    doajax('add_employee',td);
                }
                for(var i=0;i<data['staff'].length;i++){ 
                    var d = data['staff'][i].split('|')
                    var td = {'ename':d[0],'brname':brname,'bsname':bsname,'idcode':d[1],'pwcode':d[2],
                            'permissions':['view','create','edit'],'emptype':'server'}
                    doajax('add_employee',td);
                }
                for(var i=0;i<data['menu'].length;i++){ 
                    var d = data['menu'][i].split('|')
                    var td = {'miname':d[0],'brname':brname,'bsname':bsname,'creator':'admin','description':d[1],
                            'price':d[2]}
                    doajax('add_menuitem',td);
                }
                get_businesses_info();
                confirm('done');
            },true);    
        },true);
    }
    
    
    
    
//DIALOG BOXES

    var station_open = function(){
    
        var dtext = $('<div  title="Logging in a Station"><h2>Please enter your PIN</h2></div>');
        $('<label>Login</label><br/><input type="text" id="pin" size="20" ></input><br/>').appendTo(dtext);
        
        var bs_div = $('<div>Business:</div>');
        var bs_selec = $('<select id="bs_select">'+makeoptions(keys(businesses_info))+'</select>'); 
        bs_selec.appendTo(bs_div);
        bs_div.appendTo(dtext);
        
        var br_div = $('<div>Branch:</div>');
        var br_selec = $('<select id="br_selec">'+makeoptions(['...'])+'</select>');  
        br_selec.appendTo(br_div);
        br_div.appendTo(dtext);
                
        $(bs_selec).change(function(){
            var ut = $(bs_selec).val();
            $(br_selec).html(makeoptions(businesses_info[ut]));   
        })
        $(bs_selec).change();
      
        dtext.dialog({
            height: 300,width:350,modal: true,overlay: {backgroundColor: '#000',opacity: 0.5},
            buttons: {
                Cancel: function() {$(this).dialog('destroy').remove();},
                'Login': function() {
                    doajax('station_open',{'brname':$(br_selec).val(),'bsname':$(bs_selec).val(),'pin':$('#pin').val()},function(data){
                        
                        
                        db.execute('CREATE TABLE if not EXISTS branch_info (br_name TEXT, bs_name TEXT, station_open INTEGER, user_rev INTEGER, menu_rev INTEGER)');
                        rs = db.execute('SELECT * from branch_info');
                        var d = fetchall(rs);
                        if(d.length==2 && d[1][0]==data.br_name && d[1][0]==data.bs_name){
                            db.execute('UPDATE branch_info SET station_open= ?',[1])
                        }
                        else{
                            db.execute('DELETE FROM  branch_info');
                            db.execute('INSERT INTO branch_info (br_name,bs_name,station_open,user_rev,menu_rev) VALUES (?,?,?,?,?)',[data.br_name,data.bs_name,1,0,0])
                            
                            db.execute('CREATE TABLE if not EXISTS users_info (e_name TEXT,e_idcode TEXT, e_pin TEXT)');
                            db.execute('DELETE FROM  users_info');
                            db.execute('CREATE TABLE if not EXISTS menu_info (dkey TEXT, name TEXT,description TEXT, price REAL)');
                            db.execute('DELETE FROM  menu_info');
                            db.execute('CREATE TABLE if not EXISTS checks_info (created_at INTEGER,created_by TEXT, killed_at INTEGER DEFAULT 0, killed_by TEXT DEFAULT "",rev INTEGER,local_edit TEXT,sale_total REAL,number_guests INTEGER DEFAULT 1)');
                            db.execute('DELETE FROM  checks_info');
                            db.execute('CREATE TABLE if not EXISTS items_info (ordered_at INTEGER,ordered_by TEXT,name TEXT, price REAL, quantity INTEGER, checkid INTEGER,rev INTEGER,local_edit TEXT)');
                            db.execute('DELETE FROM  items_info');
                        }
                        
                        
                        db.execute('CREATE TABLE if not EXISTS login_info (e_name TEXT,e_idcode TEXT, login_ms INTEGER, logout_ms INTEGER)');
                        db.execute('DELETE FROM  login_info');
                        db.execute('INSERT INTO login_info (e_name,e_idcode,login_ms,logout_ms) VALUES (?,?,?,?)',[data.e_name,data.e_idcode,new Date().getTime(), null ])

                        window.location='/pos';
                        $(dtext).dialog('destroy').remove();
                    });
                }
            },
            
            close:function(){$(this).dialog('destroy').remove()}
        });
    
    }
    
    var clear_db = function(){
        db.execute('DROP TABLE users_info');
        db.execute('DROP TABLE menu_info');
        db.execute('DROP TABLE checks_info');
        db.execute('DROP TABLE items_info');
        db.execute('DROP TABLE branch_info');
    
    }
//TOOLS    
   
    
    
    
    get_businesses_info()
})
                               
